package com.wx.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wx.entity.SysWxuser;
import com.wx.entity.WxImageReply;
import com.wx.entity.WxImageReplyExample;
import com.wx.service.WxImageReplyService;
import com.zyht.common.AbsManageBaseController;
import com.zyht.common.bean.PropertiesUtilBean.PropertiesUtils;
import com.zyht.website.entity.SysUser;

/**
 * <b>描述：</b> 微信图片回复功能<br/>
 * <b>作者：</b>zrb <br/>
 * <b>修改日期：</b>2016年5月19日 - 上午10:00:43<br/>
 */
@RequestMapping("admin/wx/reply/img")
@Controller
public class WxImageReplyController extends AbsManageBaseController {

	@Resource
	private WxImageReplyService wxImageReplyService;

	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年5月19日 - 上午10:05:45<br/>
	 * <b>功能说明：</b> 图片回复首页<br/>
	 * 
	 * @param currentPage
	 * @param model
	 * @return
	 */
	@RequestMapping("index")
	public String index(Integer currentPage, String keyword, HttpServletRequest request, Model model) {
		if (null == currentPage) {
			currentPage = 1;
		}
		PageHelper.startPage(currentPage, getPageSize());
		WxImageReplyExample example = new WxImageReplyExample();
		com.wx.entity.WxImageReplyExample.Criteria cria = example.createCriteria();
		cria.andWxidEqualTo(getWxId(request));
		if (!StringUtils.isEmpty(keyword)) {
			cria.andKeywordLike("%" + keyword + "%");
		}
		example.setOrderByClause("create_time desc");
		Page<WxImageReply> listPage = (Page<WxImageReply>) this.wxImageReplyService.list(example);
		List<WxImageReply> list = listPage.getResult();
		int total = (int) listPage.getTotal();
		com.zyht.common.util.Page<WxImageReply> page = new com.zyht.common.util.Page<>(total, currentPage,
				getPageSize());
		page.setRecordList(list);
		model.addAttribute("page", page);
		model.addAttribute("keyword", keyword);
		return "basic/wx_imagereply/index";
	}

	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年6月2日 - 下午5:34:51<br/>
	 * <b>功能说明：</b> 跳转编辑页面<br/>
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String toEditPage(@RequestParam(value = "p", required = false) Integer id, Model model) {
		if (null != id) {
			WxImageReply bean = this.wxImageReplyService.findOneById(id);
			model.addAttribute("bean", bean);
		}
		return "basic/wx_imagereply/edit";
	}

	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年6月2日 - 下午5:58:51<br/>
	 * <b>功能说明：</b> 保存或更新<br/>
	 * 
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	public String saveOrUpdate(WxImageReply entity, HttpSession session, HttpServletRequest request) {
		String sessionWxKey = PropertiesUtils.getConfig("session_wx_key");
		SysWxuser sysWxUser = (SysWxuser) getSessionValue(session, sessionWxKey);
		String bp = session.getServletContext().getRealPath("/");
		SysUser sysUser = getSessionUser(request);
		if (entity.getId() == null) {
			entity.setUid(sysUser.getId());
		}
		this.wxImageReplyService.updateWxImageReplyBean(entity, sysWxUser, bp);
		return "redirect:/admin/wx/reply/img/msg/0";
	}

	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年6月2日 - 下午6:17:11<br/>
	 * <b>功能说明：</b> delete方法的删除，h5支持<br/>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.DELETE)
	public String remove(@RequestParam("p") Integer id) {
		if (null != id) {
			this.wxImageReplyService.deleteById(id);
		}
		return "redirect:/admin/wx/reply/img/msg/0";
	}

	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年6月2日 - 下午6:17:11<br/>
	 * <b>功能说明：</b>删除<br/>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "d{id}", method = RequestMethod.GET)
	public String delete(@PathVariable("id") Integer id, HttpSession session) {
		if (null != id) {
			String sessionWxKey = PropertiesUtils.getConfig("session_wx_key");
			SysWxuser sysWxUser = (SysWxuser) getSessionValue(session, sessionWxKey);
			WxImageReply bean = this.wxImageReplyService.findOneById(id);
			// 删除本地文件
			String imagePath = bean.getImagePath();
			String bp = session.getServletContext().getRealPath("/");
			Path path = Paths.get(bp, imagePath);
			try {
				Files.deleteIfExists(path);
			} catch (IOException e1) {
				e1.printStackTrace();
				logger.error("文件[" + imagePath + "]删除异常，IO exception!");
			}
			this.wxImageReplyService.deleteWxImageReplyBean(id, sysWxUser);
		}
		return "redirect:/admin/wx/reply/img/msg/0";
	}

	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年6月2日 - 下午6:17:11<br/>
	 * <b>功能说明：</b>详情x<br/>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "p{id}", method = RequestMethod.GET)
	public String show(@PathVariable("id") Integer id, Model model) {
		if (null != id) {
			WxImageReply bean = this.wxImageReplyService.findOneById(id);
			model.addAttribute("bean", bean);
		}
		return "basic/wx_imagereply/detail";
	}

	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年6月2日 - 下午5:58:33<br/>
	 * <b>功能说明：</b> 消息处理<br/>
	 * 
	 * @param code
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("msg/{e}")
	public String dealMessage(@PathVariable(value = "e") Integer code, HttpServletRequest request, Model model) {
		if (null != code && 0 == code.intValue()) {
			model.addAttribute("msg", getMessage(request, "sys_common_operation_ok"));
		} else {
			model.addAttribute("msg", getMessage(request, "sys_common_operation_fail"));
		}
		model.addAttribute("redirect", "admin/wx/reply/img/index");
		return "message";
	}
}
